#event study functions----------------------------------------------------------------------
make_twfe_event_study <- function(df, govt_prop, pop_prop, occ_filter, lab1, lab2){
  var_govt_prop = enquo(govt_prop)
  var_pop_prop = enquo(pop_prop)
  
  df <-
    df %>%
    filter_(occ_filter) %>%
    mutate(
      #outcome variables
      govt_prop = !!var_govt_prop,
      pop_prop = !!var_pop_prop,
      over_prop = govt_prop - pop_prop,
      
      #independent variables    
      ever_treat = ((!is.na(treat))*1),
      distance = case_when(ever_treat == 0 ~ 0,
                           TRUE ~ as.numeric(YEAR) - as.numeric(all)),
      grouped_distance = floor(distance/10))
  
  feols(govt_prop ~ i(grouped_distance, ever_treat, ref = -1) + pop_prop | city + YEAR, cluster = ~city, data = df) %>%
    tidy() %>%
    select(term, estimate, std.error) %>%
    add_row(term = "-1",  estimate = 0, std.error = NA_real_) %>%
    mutate(val = str_remove_all(term, "[:alpha:]|\\_|\\:"),
           lab1 = lab1,
           lab2 = lab2)
}

bc_out4 <- make_twfe_event_study(analysis_df_6, govt_irish_x_literate, irish_x_literate, 'occ == "blue_collar"', lab1 = "Irish", lab2 = "Blue Collar") %>% mutate(lit = "Literate")
bc_out5 <- make_twfe_event_study(analysis_df_6, govt_german_x_literate, german_x_literate, 'occ == "blue_collar"', lab1 = "German", lab2 = "Blue Collar") %>% mutate(lit = "Literate")

bc_out4_nl <- make_twfe_event_study(analysis_df_6, govt_irish_x_nonliterate, irish_x_literate, 'occ == "blue_collar"', lab1 = "Irish", lab2 = "Blue Collar") %>% mutate(lit = "Illiterate")
bc_out5_nl <- make_twfe_event_study(analysis_df_6, govt_german_x_nonliterate, german_x_literate, 'occ == "blue_collar"', lab1 = "German", lab2 = "Blue Collar") %>% mutate(lit = "Illiterate")
 

irish_german_literacy_plot <-
  bind_rows(bc_out4, bc_out4_nl,
          bc_out5, bc_out5_nl) %>%
  mutate(val = as.numeric(as.character(val))) %>%
  mutate(pre_post = (val > -1)*1) %>%
  mutate(lit = factor(lit, levels = c("Literate", "Illiterate"))) %>%
  #mutate(lab1 = factor(lab1, levels = c("White Foreign Born", "Black Native Born", "White Native Born"))) %>%
  ggplot(aes(x=val, y = estimate, shape = lit, color = lit, group = lit, alpha = factor(pre_post))) +
  geom_point(position = position_dodge(width = 0.5)) +
  geom_errorbar(aes(ymin = estimate - 1.96*std.error, ymax = estimate + 1.96*std.error), 
                position = position_dodge(width = 0.5),
                width = 0) +
  geom_hline(yintercept = 0, linetype = "dotted") +
  facet_wrap(lab1~.) +
  theme_bw() +
  scale_colour_grey() +
  xlab("Decades Before/After Civil Service Reform") +
  ylab("Effect of Civil Service Reform on Representation") +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        axis.line.y.left = element_blank(),
        legend.position = "bottom",
        axis.ticks.y = element_blank(),
        strip.background = element_blank(),
        legend.title = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank()) +
  scale_x_continuous(breaks = seq(-4, 4, by = 1), 
                     limits = c(-4.5, 4.5)) +
  scale_alpha_manual(values = c(0.4, 1)) +
  guides(alpha = FALSE) +
  theme(text=element_text(size=9)) +
  scale_y_continuous(limits = c(-0.1, 0.1))

#ggsave('../Apps/Overleaf/merit paper/outputs/irish_german_literacy_plot.png', plot = irish_german_literacy_plot, width=6, height=3.25)
ggsave('./replication_file/_5_outputs/figures/figure_7.png', plot = irish_german_literacy_plot, width=6, height=4)
